home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / cweb31.zip / EXAMPLES / WORDTEST.W < prev    next >
Text File  |  1993-06-10  |  21KB  |  540 lines

  1. \datethis
  2. @* Introduction. This program is a simple filter that sorts and outputs all
  3. lines of input that do not appear in a given set of sorted files. It is called
  4. {\tt wordtest} because each line of input is considered to be a `word' and
  5. each of the sorted files is considered to be a 'dictionary'. Words are
  6. output when they don't appear in any given dictionary.
  7.  
  8. The character set and alphabetic order are flexible. Every 8-bit
  9. character is mapped into an integer called its {\it ord}. A character
  10. is called a {\it null\/} if its ord is zero; such characters are
  11. discarded from the input. A character is called a {\it break\/} if
  12. its ord is negative; such characters break the input into so-called words.
  13. Otherwise a character's ord is positive, and the character is called a
  14. {\it letter}. One letter precedes another in alphabetic order if and only
  15. if it has a smaller ord. Two letters are considered identical, for
  16. purposes of sorting, if their ords are the same.
  17.  
  18. The null character |'\n'| must have ord~0; thus, it must remain null.
  19. Otherwise the ord mapping is arbitrary. If the user doesn't specify
  20. any special mapping, the default ord table simply maps every 8-bit
  21. character code into itself, considering characters to be unsigned char
  22. values in the range 0--255, except that ASCII codes {\tt a-z} are
  23. mapped into the corresponding codes for {\tt A-Z}, and newline is a
  24. break character. Optional command-line arguments, described below, can
  25. change this default mapping to any other desired scheme.
  26.  
  27. A word is any nonempty sequence of letters that is immediately preceded
  28. and followed by break characters, when nulls are ignored. Technically
  29. speaking, we pretend that a break character is present at the beginning of a
  30. file but not at the end; thus, all letters following the final break character
  31. of a file are ignored, if any such letters are present. Two words are
  32. {\it equivalent\/} to each other if their letters have the same sequence
  33. of ord values. If two or more words of the input are equivalent, only
  34. the first will be output, and it will be output only if it is not
  35. equivalent to any word in the given dictionary files. Words in each
  36. dictionary are assumed to be in lexicographic order and to contain no
  37. nulls. Words in the output file will satisfy these conditions; therefore
  38. {\tt wordtest} can be used to generate and update the dictionaries it needs.
  39. Notice that if no dictionaries are given, {\tt wordtest} will act as a
  40. sorting routine that simply discards nulls and duplicate lines.
  41.  
  42. @ The \UNIX/ command line `{\tt wordtest} {\tt [options]} {\tt [dictionaries]}'
  43. is interpreted by executing option commands from left to right and then by
  44. regarding any remaining arguments as the names of dictionary files.
  45.  
  46. Most of the option commands are designed to specify the |ord| table.
  47. Initially |ord[c]=c| for each unsigned char code~|c|. The command
  48. $$\line{\hskip5em\tt-b\it string\hfil}$$
  49. makes every character in the string a break character. If the string is
  50. empty, {\tt-b} makes every nonnull character a break (i.e., it sets
  51. |ord[c]=-1| for |1<=c<=255|). The command
  52. $$\line{\hskip5em\tt-n\it string\hfil}$$
  53. makes every character in the string a null character. If the string is
  54. empty, {\tt-n} makes every character null. The command
  55. $$\line{\hskip5em\tt-a\it string\hfil}$$
  56. sets the ord of the $k$th element of the string equal to $\delta+k$,
  57. where $\delta$ is an offset value (normally zero). The command
  58. $$\line{\hskip5em\tt-d\it offset\hfil}$$
  59. sets the value of $\delta$; the offset should be a decimal integer between
  60. 0 and 255.
  61.  
  62. There is also an option that has no effect on the |ord| table:
  63. $$\line{\hskip5em\tt-m\it length\hfil}$$
  64. defines the length of the longest word. If any word of a file has
  65. more than this many characters, a break is artificially inserted
  66. so that a word of this maximum length is obtained. The default value is 50.
  67. The maximum legal value is 1000.
  68.  
  69. If the given options do not specify at least one break character,
  70. {\tt wordtest} applies the option commands
  71. $$\vbox{\line{\hskip5em\.{-b"\\}\hfil}
  72. \line{\.{" -d64 -a"abcdefghijklmnopqrstuvwxyz"}\hfil}}$$
  73. which generate the default mapping mentioned above (unless other ords were
  74. changed).
  75.  
  76. The program is designed to run fastest when there are at most two
  77. dictionary files (usually one large system dictionary and another
  78. personalized one), although it places no limit on the actual number of
  79. dictionaries that can be mentioned on the command line. Users who want
  80. to specify a multitude of dictionaries should ask themselves why they
  81. wouldn't prefer to merge their dictionaries together first (using
  82. {\tt wordtest}).
  83.  
  84. @d MAX_LENGTH_DEFAULT 50
  85. @d MAX_LENGTH_LIMIT 1000
  86.  
  87. @ The general organization of {\tt wordtest} is typical of applications
  88. written in \CEE/, and its approach is quite simple. If any errors are
  89. detected, an indication of the error is sent to the |stderr| file and
  90. a nonzero value is returned.
  91.  
  92. @p
  93. #include <stdio.h>
  94. @#
  95. @<Typedefs@>@;
  96. int main(argc,argv)
  97.   int argc; /* the number of command-line arguments */
  98.   char *argv[]; /* the arguments themselves */
  99. {
  100.   @<Local variables@>;
  101.   @<Scan the command line arguments@>;
  102.   @<Sort the input into memory@>;
  103.   @<Output all input words that aren't in dictionaries@>;
  104.   return 0;
  105. }
  106.  
  107. @ @<Typedefs@>=
  108. typedef unsigned char byte; /* our bytes will range from 0 to 255 */
  109.  
  110. @ @<Local variables@>=
  111. int targc; /* temporary modifications to |argc| */
  112. byte **targv; /* pointer to the current argument of interest */
  113. unsigned delta; /* the offset used in the \.{-a} and \.{-d} options */
  114. unsigned max_length=MAX_LENGTH_DEFAULT; /* longest allowable word */
  115. byte breakchar; /* break character to use in the output */
  116. int ord[256]; /* table of ord values */
  117. register int c; /* an all-purpose index */
  118. register byte *u,*v; /* pointer to current string characters */
  119.  
  120. @ We try to use newline as the output break character, if possible.
  121.  
  122. @<Scan the command line arguments@>=
  123. for (c=0;c<256;c++) ord[c]=c;
  124. delta=0;
  125. targc=argc-1;@+targv=(byte**)argv+1;
  126. while (targc && **targv=='-') {
  127.   @<Execute the option command |targv|@>;
  128.   targc--;@+targv++;
  129. }
  130. if (ord['\n']<0) breakchar='\n';
  131. else {
  132.   breakchar='\0';
  133.   for (c=255;c;c--) if (ord[c]<0) breakchar=c;
  134.   if (!breakchar) @<Set up the default ords@>;
  135. }
  136. @<Allocate data structures for a total of |targc| files@>;
  137. for (;targc;targc--,targv++) @<Open the dictionary file named |*targv|@>;
  138.  
  139. @ @<Execute the option...@>=
  140. switch((*targv)[1]) {
  141. case 'a': for (c=delta,u=*targv+2;*u;u++) ord[*u]=++c;@+break;
  142. case 'b': if ((*targv)[2]) for (u=*targv+2;*u;u++) ord[*u]=-1;
  143.   else for (c=1;c<256;c++) ord[c]=-1;
  144.   break;
  145. case 'n': if ((*targv)[2]) for (u=*targv+2;*u;u++) ord[*u]=0;
  146.   else for (c=1;c<256;c++) ord[c]=0;
  147.   break;
  148. case 'd': if (sscanf((char*)*targv+2,"%u",&delta)==1 && delta<256) break;
  149.   goto print_usage;
  150. case 'm': if (sscanf((char*)*targv+2,"%u",&max_length)==1 &
  151.     max_length<=MAX_LENGTH_LIMIT) break;
  152.   goto print_usage;
  153. default: print_usage: fprintf(stderr,
  154.   "Usage: %s {-{{a|b|n}string|{d|m}number}}* dictionaryname*\n",*argv);
  155.   return-1;
  156. }
  157.  
  158. @ @<Set up the default ords@>=
  159. {
  160.   ord['\n']=-1; /* newline is break character */
  161.   breakchar='\n';
  162.   for (c=1;c<=26;c++) ord['a'-1+c]='A'-1+c;
  163. }
  164.  
  165. @*Treaps. The most interesting part of this program is its sorting algorithm,
  166. which is based on the ``treap'' data structure of Aragon and Seidel
  167. [{\sl 30th IEEE Symposium on Foundations of Computer Science\/} (1989),
  168. 540--546].
  169. @^Aragon, Cecilia Rodriguez@>@^Seidel, Raimund@>
  170. A treap is a binary tree whose nodes have two key fields. The primary
  171. key, which in our application is a word from the input, obeys
  172. tree-search order: All descendants of the left child of node~$p$ have
  173. a primary key that is less than the primary key of~$p$, and all descendants
  174. of its right child have a primary key that is greater. The secondary key,
  175. which in our application is a unique pseudorandom integer attached to
  176. each input word, obeys heap order: The secondary key of~$p$'s children
  177. is greater than $p$'s own secondary key.
  178.  
  179. A given set of nodes with distinct primary keys and distinct secondary
  180. keys can be made into a treap in exactly one way. This unique treap
  181. can be obtained, for example, by using ordinary tree insertion with
  182. respect to primary keys while inserting nodes in order of their
  183. secondary keys. It follows that, if the secondary keys are random,
  184. the binary tree will almost always be quite well balanced.
  185.  
  186. We will compute secondary keys as unsigned long integers, assigning
  187. the key $(cn)\bmod 2^{32}$ to the $n$th node, where $c$ is an odd
  188. number. This will guarantee that the secondary keys are distinct.
  189. By choosing $c$ close to $2^{32}/\phi$, where $\phi$ is the golden
  190. ratio $(1+\sqrt5\,)/2$, we also spread the values out in a fashion that
  191. is unlikely to match any existing order in the data.
  192.  
  193. @d PHICLONE 2654435769 /* $\approx 2^{32}/\phi$ */
  194.  
  195. @<Typedefs@>=
  196. typedef struct node_struct {
  197.   struct node_struct *left,*right; /* children */
  198.   byte *keyword; /* primary key */
  199.   unsigned long rank; /* secondary key */
  200. } node; /* node of a treap */
  201.  
  202. @ We want to be able to compare two strings rapidly with respect to
  203. lexicographic order, as defined by the |ord| table. This can be done
  204. if one string is delimited by |'\0'| as usual, while the other is
  205. delimited by a break character. Then we are sure to have an unequal
  206. comparison, and the inner loop is fast.
  207.  
  208. Here is a routine that checks to see if a word is already present in the
  209. treap.  The word is assumed to be in |buffer|, terminated by |breakchar|.
  210. The words in the treap are terminated by nulls. The
  211. treap is accessed by means of |root|, a pointer to its root node.
  212.  
  213. @<Search for |buffer| in the treap; |goto found| if it's there@>=
  214. {@+register node *p=root;
  215.   while (p) {
  216.     for (u=buffer,v=p->keyword;ord[*u]==ord[*v];u++,v++) ;
  217.     if (*v=='\0' && *u==breakchar) goto found;
  218.     if (ord[*u]<ord[*v]) p=p->left;
  219.     else p=p->right;
  220.   }
  221. }
  222.  
  223. @ We don't need to insert nodes into the treap as often as we need to
  224. look words up, so we don't mind repeating the comparisons already made
  225. when we discover that insertion is necessary. (Actually a more comprehensive
  226. study of this tradeoff ought to be done. But not today; I am trying
  227. here to keep the program short and sweet.)
  228.  
  229. The insertion algorithm proceeds just as the lookup algorithm until
  230. we come to a node whose rank is larger than the rank of the node
  231. to be inserted. We insert the new node in its place, then split the
  232. old node and its descendants into two subtrees that will become the
  233. left and right subtrees of the new node.
  234.  
  235. @<Insert the |buffer| word into the treap@>=
  236. {@+register node *p,**q,**qq,*r;
  237.   current_rank += PHICLONE; /* unsigned addition mod $2^{32}$ */
  238.   p=root;@+q=&root;
  239.   while (p) {
  240.     if (p->rank>current_rank) break; /* end of the first phase */
  241.     for (u=buffer,v=p->keyword;ord[*u]==ord[*v];u++,v++) ;
  242.     if (ord[*u]<ord[*v]) q=&(p->left), p=*q;
  243.     else q=&(p->right), p=*q;
  244.   }
  245.   @<Set |r| to the address of a new node, and move |buffer| into it@>;
  246.   r->rank=current_rank;
  247.   *q=r; /* link the new node into the tree */
  248.   @<Split subtree |p| and attach it below node |r|@>;
  249. }
  250.  
  251. @ @<Local...@>=
  252. unsigned long current_rank=0; /* pseudorandom number */
  253.  
  254. @ At this point |p| may already be empty. If not, we can hook its
  255. parts together easily.  (A formal proof is a bit tricky, but the computer
  256. doesn't slow down like people do when they get to a conceptually harder
  257. part of an algorithm.)
  258.  
  259. @<Split subtree |p| and attach it below node |r|@>=
  260. q=&(r->left);@+qq=&(r->right); /* slots to fill in as we split the subtree */
  261. while (p) {
  262.   for (u=buffer,v=p->keyword;ord[*u]==ord[*v];u++,v++) ;
  263.   if (ord[*u]<ord[*v]) {
  264.     *qq=p;
  265.     qq=&(p->left);
  266.     p=*qq;
  267.   } else {
  268.     *q=p;
  269.     q=&(p->right);
  270.     p=*q;
  271.   }
  272. }
  273. *q=*qq=NULL;
  274.  
  275. @ We allocate node memory dynamically, in blocks of 100 nodes at a time.
  276. We also allocate string memory dynamically, 1000 characters at once
  277. (in addition to space for the current string).
  278. The variable |l| will be set to the length of the word in |buffer|.
  279.  
  280. @d NODES_PER_BLOCK 100
  281. @d CHARS_PER_BLOCK 1000
  282. @d out_of_mem(x) {@+fprintf(stderr,"%s: Memory exhausted!\n",*argv);
  283.                     return x;@+}
  284.  
  285. @<Set |r| to the address of a new node, and move |buffer| into it@>=
  286. if (next_node==bad_node) {
  287.   next_node=(node*)calloc(NODES_PER_BLOCK,sizeof(node));
  288.   if (next_node==NULL) out_of_mem(-2);
  289.   bad_node=next_node+NODES_PER_BLOCK;
  290. }
  291. r=next_node++;
  292. @<Move |buffer| to a new place in the string memory, and make
  293.   |r->keyword| point to it@>;
  294.  
  295. @ @<Move |buffer| to a new place...@>=
  296. if (next_string+l+1>=bad_string) {@+int block_size=CHARS_PER_BLOCK+l+1;
  297.   next_string=(byte*)malloc(block_size);
  298.   if (next_string==NULL) out_of_mem(-3);
  299.   bad_string=next_string+block_size;
  300. }
  301. r->keyword=next_string;
  302. for (u=buffer,v=next_string;ord[*u]>0;u++,v++) *v=*u;
  303. *v='\0';
  304. next_string=v+1;
  305.  
  306. @ We had better define the variables we've been assuming in these
  307. storage allocation routines.
  308.  
  309. @<Local variables@>=
  310. node *next_node=NULL, *bad_node=NULL;
  311. byte *next_string=NULL, *bad_string=NULL;
  312. node *root=NULL;
  313. byte *buffer;
  314. int l; /* length of current string in |buffer| */
  315.  
  316. @ The mechanisms for sorting the input words are now all in place.
  317. We merely need to invoke them at the right times.
  318.  
  319. @<Sort the input into memory@>=
  320. buffer=(byte*)malloc(max_length+1);
  321. if (buffer==NULL) out_of_mem(-5);
  322. while (1) {
  323.   @<Set |buffer| to the next word from |stdin|; |goto done| if file ends@>;
  324.   if (l) {
  325.    @<Search for |buffer| in the treap; |goto found| if it's there@>;
  326.    @<Insert the |buffer| word into the treap@>;
  327.  found:;    
  328.   }
  329. }
  330. done:;
  331.  
  332. @ @<Set |buffer| to the next word from |stdin|...@>=
  333. u=buffer;@+l=0;
  334. while (l<max_length) {
  335.   c=getchar();
  336.   if (c==EOF) {
  337.     if (ferror(stdin)) {
  338.       fprintf(stderr,"%s: File read error on standard input!\n",*argv);
  339.       return -6;
  340.     }
  341.     goto done; /* end of file; the current word, if any, is discarded */
  342.   }
  343.   if (ord[c]<=0) {
  344.     if (ord[c]<0) break;
  345.   } else {
  346.     *u++=(byte)c;
  347.     l++;
  348.   }
  349. }
  350. *u=breakchar;
  351.  
  352. @ At the end we want to traverse the treap in symmetric order, so that
  353. we see its words in alphabetic order. We might as well destroy the
  354. treap structure as we do this. During this phase, |root| will point
  355. to a stack of nodes that remain to be visited (followed by traversal
  356. of their right subtrees).
  357.  
  358. @<Output all input words that aren't in dictionaries@>=
  359. if (root!=NULL) {@+register node *p,*q;
  360.   p=root;
  361.   root=NULL;
  362.   while (1) {
  363.     while (p->left!=NULL) {
  364.       q=p->left;
  365.       p->left=root; /* |left| links are now used for the stack */
  366.       root=p;
  367.       p=q;
  368.     }
  369. visit: @<Output |p->keyword|, if it's not in the dictionaries@>;
  370.     if (p->right==NULL) {
  371.       if (root==NULL) break; /* the stack is empty, we're done */
  372.       p=root;
  373.       root=root->left; /* pop the stack */
  374.       goto visit;
  375.     } else p=p->right;
  376.   }
  377. }
  378.  
  379. @* The dictionaries. So now all we have to do is provide a mechanism
  380. for reading the words in the dictionaries. The dictionaries are sorted,
  381. and by now the input words have been sorted too.
  382. So we need only scan through the
  383. dictionaries once; we'll try to zoom through as quickly as possible.
  384.  
  385. First we need data structures. There will be an array of pointers to filenodes,
  386. for all dictionary files currently open. Each filenode will contain
  387. a buffer of size |BUFSIZ+1| for raw input bytes not yet scanned,
  388. as well as a buffer of size |MAX_LENGTH_LIMIT+1| for the current word
  389. being considered.
  390.  
  391. @<Typedefs@>=
  392. typedef struct filenode_struct {
  393.   struct filenode_struct *link; /* pointer to next open file */
  394.   FILE *dfile; /* dictionary file */
  395.   byte buf[BUFSIZ+1], curword[MAX_LENGTH_LIMIT+1];
  396.   byte *pos; /* current position in |buf| */
  397.   byte *limit; /* end of input bytes in |buf| */
  398.   byte *endword; /* the first break character in |curword| */
  399. } filenode;
  400.  
  401. @ @<Allocate data structures...@>=
  402. if (targc) {
  403.   curfile=(filenode*)calloc(targc,sizeof(filenode));
  404.   if (curfile==NULL) out_of_mem(-7);
  405.   for (f=curfile;f<curfile+targc-1;f++) f->link=f+1;
  406.   f->link=curfile; /* circular linking */
  407. } else curfile=NULL;
  408.  
  409. @ @<Local...@>=
  410. filenode *curfile; /* current filenode of interest */
  411. filenode *f; /* temporary register for filenode list processing */
  412.  
  413. @ @<Open the dictionary file named |*targv|@>=
  414. {
  415.   curfile->dfile=fopen((char*)*targv,"r");
  416.   if (curfile->dfile==NULL) {
  417.     fprintf(stderr,"%s: Can't open dictionary file %s!\n",*argv,(char*)*targv);
  418.     return -8;
  419.   }
  420.   curfile->pos=curfile->limit=curfile->buf; /* |buf| is empty */
  421.   curfile->buf[0]='\0';
  422.   curfile->endword=curfile->curword; /* |curword| is empty too */
  423.   curfile->curword[0]=breakchar;
  424.   curfile=curfile->link; /* move to next filenode */
  425. }
  426.  
  427. @ We will implicitly merge the dictionaries together by using a brute force
  428. scheme that works fine when there are only a few of them. Namely,
  429. |curfile| will point to a file having the currently smallest
  430. current word. To get to the next word of the merge, we advance to the
  431. next word in that file, comparing it with the current words of the
  432. other files to see if |curfile| should switch to one of them.
  433. When we get to the end of a file, its filenode simply leaves the circular
  434. list. Eventually the list will be empty, and we will set |curfile| to
  435. |NULL|; we will then have seen all the dictionary words in order.
  436.  
  437. @ @<Output |p->keyword|, if it's not in the dictionaries@>=
  438. while (curfile!=NULL) {
  439.   for (u=p->keyword,v=curfile->curword;ord[*u]==ord[*v];u++,v++) ;
  440.   if (*u=='\0' && *v==breakchar) goto word_done;
  441.     /* we found it in the dictionary */
  442.   if (ord[*u]<ord[*v]) break; /* we didn't find it */
  443.   @<Advance to the next dictionary word@>;
  444. }
  445. @<Print |p->keyword| and |breakchar| on |stdout|@>@;
  446. word_done:;
  447.  
  448. @ @<Print |p->keyword| and |breakchar| on |stdout|@>=
  449. for (u=p->keyword;*u;u++) putchar(*u);
  450. putchar(breakchar);
  451.  
  452. @ @<Advance...@>=
  453. @<Read a new word into |curfile->curword|, as fast as you can@>;
  454. @<Adjust |curfile|, if necessary, to point to a file with minimal
  455.   |curword|@>;
  456.  
  457. @ The dictionaries are supposed to be in order, and they shouldn't
  458. contain nulls. But if they fail to meet these criteria, we don't want
  459. {\tt wordtest} to crash; it should just run more slowly and/or more
  460. peculiarly.
  461.  
  462. The logic of the code here removes null characters, at the cost of speed.
  463. If the dictionary contains words out of order, say $\alpha>\beta$ where
  464. $\alpha$ precedes $\beta$ in the file, the effect will be as if $\beta$
  465. were not present. (In particular, if the dictionary would happen to have a null
  466. word because of a break character inserted by our |max_length| logic,
  467. that null word would cause no harm, because a null word is always less than
  468. any nonnull word.)
  469.  
  470. A null character always appears in |curfile->limit|.
  471.  
  472. @<Read a new word into |curfile->curword|...@>=
  473. v=curfile->curword;
  474. l=max_length; /* here |l| represents max characters to put in |curword| */
  475. while (1) {@+register byte *w=curfile->limit;
  476.   u=curfile->pos;
  477.   if (u+l>=w)
  478.     while (ord[*u]>0) *v++=*u++; /* this is the inner loop */
  479.   else {
  480.     w=u+l;
  481.     c=*w;
  482.     *w='\0'; /* temporarily store a null to avoid overlong string */
  483.     while (ord[*u]>0) *v++=*u++; /* this too is the inner loop */
  484.     *w=c; /* restore the damaged byte */
  485.   }
  486.   if (ord[*u]<0) {
  487.     curfile->pos=u+1; /* good, we found the next break character */
  488.     break;
  489.   }
  490.   l-=u-curfile->pos;
  491.   if (l==0) { /* |max_length| reached */
  492.     curfile->pos=u;
  493.     break;
  494.   }
  495.   if (u==w) { /* we're at |curfile->limit| */
  496.     @<Refill |curfile->buf|; or remove the current file from the
  497.       circular list and |goto update_done|, if it has ended@>;
  498.   } else curfile->pos=u+1; /* bypass a null character in the dictionary */
  499. }
  500. curfile->endword=v;
  501. *v=breakchar;
  502. update_done:;
  503.  
  504. @ @<Refill |curfile->buf|...@>=
  505. if (ferror(curfile->dfile)) {
  506.   fprintf(stderr,"%s: File read error on dictionary file!\n",*argv);
  507.   return -9;
  508. }
  509. if (feof(curfile->dfile)) {
  510.   f=curfile->link;
  511.   if (f==curfile) curfile=NULL; /* the last dictionary file has ended */
  512.   else {
  513.     while (f->link!=curfile) f=f->link;
  514.     f->link=curfile->link; /* remove a filenode from the circular list */
  515.     curfile=f; /* and point to one of the remaining filenodes */
  516.   }
  517.   goto update_done;
  518. }
  519. curfile->limit=curfile->buf+fread(curfile->buf,1,BUFSIZ,curfile->dfile);
  520. *curfile->limit='\0';
  521. curfile->pos=curfile->buf;
  522.  
  523. @ @<Adjust |curfile|, if necessary...@>=
  524. if (curfile!=NULL) {@+filenode *sentinel=curfile;
  525.   for (f=curfile->link;f!=sentinel;f=f->link)
  526.     @<Change |curfile| to |f| if |f->curword<curfile->curword|@>;    
  527. }
  528.  
  529. @ @<Change |curfile| to |f| if |f->curword<curfile->curword|@>=
  530. {
  531.   *f->endword='\0';
  532.   for (u=f->curword,v=curfile->curword;ord[*u]==ord[*v];u++,v++) ;
  533.   if (ord[*u]<ord[*v]) curfile=f;
  534.   *f->endword=breakchar;
  535. }
  536.  
  537. @* Index. Here is a list of the identifiers used by {\tt wordtest},
  538. showing the sections in which they appear, underlined at points
  539. of definition.
  540.